{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T07:02:26.173012Z",
     "start_time": "2025-05-15T07:02:26.170354Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "\n",
    "# 创建一个决策树分类器\n",
    "tree_clf = DecisionTreeClassifier()\n"
   ],
   "id": "f132e4555c342075",
   "outputs": [],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T07:02:26.179425Z",
     "start_time": "2025-05-15T07:02:26.176017Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "# 创建一个随机森林分类器\n",
    "rf_clf = RandomForestClassifier(n_estimators=100)  # n_estimators是决策树的数量\n"
   ],
   "id": "ac04cc8b9be1115b",
   "outputs": [],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T07:02:26.195430Z",
     "start_time": "2025-05-15T07:02:26.192953Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 依然使用决策树作为基础学习器，但通常树会很浅\n",
    "tree_clf = DecisionTreeClassifier(max_depth=1)\n"
   ],
   "id": "1a346e570209b16b",
   "outputs": [],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T07:02:26.211946Z",
     "start_time": "2025-05-15T07:02:26.209407Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "\n",
    "# 创建一个AdaBoost分类器\n",
    "ada_clf = AdaBoostClassifier(n_estimators=100)\n"
   ],
   "id": "def05732104a5c3e",
   "outputs": [],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T07:02:26.228973Z",
     "start_time": "2025-05-15T07:02:26.226333Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "\n",
    "# 创建一个Gradient Boosting分类器\n",
    "gb_clf = GradientBoostingClassifier(n_estimators=100)\n"
   ],
   "id": "204148901207d77c",
   "outputs": [],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T07:02:26.246780Z",
     "start_time": "2025-05-15T07:02:26.243915Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "# 创建多个不同的学习器\n",
    "lr_clf = LogisticRegression()\n",
    "svm_clf = SVC(probability=True)  # probability=True以启用概率估计\n",
    "knn_clf = KNeighborsClassifier()\n"
   ],
   "id": "7b688bf48dd3ef36",
   "outputs": [],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T07:02:26.264312Z",
     "start_time": "2025-05-15T07:02:26.261169Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.ensemble import StackingClassifier\n",
    "\n",
    "# 创建一个Stacking分类器\n",
    "stack_clf = StackingClassifier(\n",
    "    estimators=[('lr', lr_clf), ('svm', svm_clf), ('knn', knn_clf)],\n",
    "    final_estimator=LogisticRegression()  # 元学习器\n",
    ")\n"
   ],
   "id": "2b2f2dfcceec7799",
   "outputs": [],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T07:02:26.349616Z",
     "start_time": "2025-05-15T07:02:26.278660Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 假设X_train, y_train是训练数据和标签\n",
    "# X_test是测试数据\n",
    "\n",
    "# 训练模型\n",
    "rf_clf.fit(X_train, y_train)  # 以随机森林为例\n",
    "\n",
    "# 进行预测\n",
    "predictions = rf_clf.predict(X_test)\n"
   ],
   "id": "174e68f3434bbe26",
   "outputs": [],
   "execution_count": 29
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
